package com.manage.project.biz.controller;


import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.manage.common.utils.poi.ExcelUtil;
import com.manage.framework.aspectj.lang.annotation.Log;
import com.manage.framework.aspectj.lang.enums.BusinessType;
import com.manage.framework.web.controller.BaseController;
import com.manage.framework.web.domain.AjaxResult;
import com.manage.framework.web.page.TableDataInfo;
import com.manage.project.biz.domain.BizGonglu;
import com.manage.project.biz.domain.BizSuidao;
import com.manage.project.biz.domain.CivilEnginneeringFacility;
import com.manage.project.biz.domain.CivilEnginneeringRegularCheck;
import com.manage.project.biz.service.IBizGongluService;
import com.manage.project.biz.service.IBizSuidaoService;
import com.manage.project.biz.service.ICivilEnginneeringFacilityService;
import com.manage.project.biz.service.ICivilEnginneeringRegularCheckService;
import com.manage.project.system.domain.SysNotice;
import com.manage.project.system.service.ISysNoticeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
/**
 * 土建结构定期检查Controller
 *
 * @author 小赵
 * @date 2022-04-25
 */
@RestController
@RequestMapping("/civil/regularcheck")
public class CivilEnginneeringRegularCheckController extends BaseController
{
    @Autowired
    private ICivilEnginneeringRegularCheckService civilEnginneeringRegularCheckService;
    @Autowired
    private IBizGongluService roadInfoService;
    @Autowired
    private ISysNoticeService noticeService;
    @Autowired
    private IBizSuidaoService iBizSuidaoService;
    @Autowired
    private ICivilEnginneeringFacilityService iCivilEnginneeringFacilityService;

    /**
     * 查询土建结构定期检查列表
     */
    @PreAuthorize("@ss.hasPermi('civil:regularcheck:list')")
    @GetMapping("/list")
    public TableDataInfo list(CivilEnginneeringRegularCheck civilEnginneeringRegularCheck)
    {
        startPage();
        List<CivilEnginneeringRegularCheck> list = civilEnginneeringRegularCheckService.selectCivilEnginneeringRegularCheckList(civilEnginneeringRegularCheck);
        return getDataTable(list);
    }

    /**
     * 导出土建结构定期检查列表
     */
/*    @PreAuthorize("@ss.hasPermi('civil:regularcheck:export')")
    @Log(title = "土建结构定期检查", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CivilEnginneeringRegularCheck civilEnginneeringRegularCheck) throws IOException {
        List<CivilEnginneeringRegularCheck> list = civilEnginneeringRegularCheckService.selectCivilEnginneeringRegularCheckList(civilEnginneeringRegularCheck);
        ExcelUtil<CivilEnginneeringRegularCheck> util = new ExcelUtil<CivilEnginneeringRegularCheck>(CivilEnginneeringRegularCheck.class);
        util.exportExcel(response, list, "土建结构定期检查数据");
    }*/

    @Log(title = "土建结构定期检查", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public  AjaxResult export(CivilEnginneeringRegularCheck civilEnginneeringRegularCheck) throws IOException {
        List<CivilEnginneeringRegularCheck> list = civilEnginneeringRegularCheckService.selectCivilEnginneeringRegularCheckList(civilEnginneeringRegularCheck);
        ExcelUtil<CivilEnginneeringRegularCheck> util = new ExcelUtil<CivilEnginneeringRegularCheck>(CivilEnginneeringRegularCheck.class);
        return  util.exportExcel(list, "土建结构定期检查数据");
    }
    /**
     * 获取土建结构定期检查详细信息
     */
    @PreAuthorize("@ss.hasPermi('civil:regularcheck:query')")
    @GetMapping(value = "/{regularCheckId}")
    public AjaxResult getInfo(@PathVariable("regularCheckId") Long regularCheckId)
    {
        return AjaxResult.success(civilEnginneeringRegularCheckService.selectCivilEnginneeringRegularCheckByRegularCheckId(regularCheckId));
    }

    /**
     * 新增土建结构定期检查
     */
    @PreAuthorize("@ss.hasPermi('civil:regularcheck:add')")
    @Log(title = "土建结构定期检查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CivilEnginneeringRegularCheck civilEnginneeringRegularCheck) {


        String roadId= civilEnginneeringRegularCheck.getRoadId();
        String roadName=civilEnginneeringRegularCheck.getRoadName();
        String tunnelName=civilEnginneeringRegularCheck.getTunnelName();
        String tunnelId=civilEnginneeringRegularCheck.getTunnelId();
        String facility=civilEnginneeringRegularCheck.getFacilityName();
        String mileageNumber=civilEnginneeringRegularCheck.getMileageNumber();
        Date time=civilEnginneeringRegularCheck.getInspectionDate();
        SimpleDateFormat strdate = new SimpleDateFormat("yyyy-MM-dd");
        String strDate= strdate.format(time);
        BizSuidao bizSuidao= iBizSuidaoService.getByTunnelNameIdAndRoadName(roadName,tunnelName,tunnelId);
        if(bizSuidao==null){
            return new AjaxResult().error("当前公路名称、隧道名称和隧道编码不对应，请核对后重新输入");
        }
        CivilEnginneeringFacility civilEnginneeringFacility=iCivilEnginneeringFacilityService.getByNameAndMileageNumber(tunnelName,facility,mileageNumber);
        if(civilEnginneeringFacility==null){
            return new AjaxResult().error(tunnelName+mileageNumber+"里程桩号位置未安装"+facility+",请联系管理员");
        }
        if(civilEnginneeringRegularCheck.getPositionNumber()!=0){
          SysNotice sysNotice= new SysNotice();
            sysNotice.setNoticeTitle("土建结构-专项检查");
            sysNotice.setNoticeType("1");
            sysNotice.setStatus("0");
            sysNotice.setNoticeContent("定期检查提交："+roadId+roadName+"-"+tunnelId+tunnelName+"-"+mileageNumber+facility+"检查任务");

//            sysNotice.setNoticeTitle(tunnelName+"土建结构"+"“"+facility+"”"+"专项检查");
//          sysNotice.setNoticeType("1");
//          sysNotice.setStatus("0");
//          sysNotice.setNoticeContent("请于"+strDate+"之后24h内完成此项检查任务");
          sysNotice.setCreateBy(getUsername());
          sysNotice.setCreateTime(time);
          noticeService.insertNotice(sysNotice);
        }

        BizGonglu roadInfo=roadInfoService.checkByRoadName(roadName);
        if(roadInfo==null) {
            return new AjaxResult().error("当前道路不存在，请输入正确的道路名称");
        }else {
            return toAjax(civilEnginneeringRegularCheckService.insertCivilEnginneeringRegularCheck(civilEnginneeringRegularCheck));
        }
    }

    /**
     * 修改土建结构定期检查
     */
    @PreAuthorize("@ss.hasPermi('civil:regularcheck:edit')")
    @Log(title = "土建结构定期检查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CivilEnginneeringRegularCheck civilEnginneeringRegularCheck)
    {
        String roadName=civilEnginneeringRegularCheck.getRoadName();
        String tunnelName=civilEnginneeringRegularCheck.getTunnelName();
        String tunnelId=civilEnginneeringRegularCheck.getTunnelId();
        String facility=civilEnginneeringRegularCheck.getFacilityName();
        String mileageNumber=civilEnginneeringRegularCheck.getMileageNumber();
        Date time=civilEnginneeringRegularCheck.getInspectionDate();
        SimpleDateFormat strdate = new SimpleDateFormat("yyyy-MM-dd");
        String strDate= strdate.format(time);
        BizSuidao bizSuidao= iBizSuidaoService.getByTunnelNameIdAndRoadName(roadName,tunnelName,tunnelId);
        if(bizSuidao==null){
            return new AjaxResult().error("当前公路名称、隧道名称和隧道编码不对应，请核对后重新输入");
        }
        CivilEnginneeringFacility civilEnginneeringFacility=iCivilEnginneeringFacilityService.getByNameAndMileageNumber(tunnelName,facility,mileageNumber);
        if(civilEnginneeringFacility==null){
            return new AjaxResult().error(tunnelName+mileageNumber+"里程桩号位置未安装"+facility+",请联系管理员");
        }
        BizGonglu roadInfo=roadInfoService.checkByRoadName(roadName);
        if(roadInfo==null) {
            return new AjaxResult().error("当前道路不存在，请输入正确的道路名称");
        }else {
            return toAjax(civilEnginneeringRegularCheckService.updateCivilEnginneeringRegularCheck(civilEnginneeringRegularCheck));
        }
        }

    /**
     * 删除土建结构定期检查
     */
    @PreAuthorize("@ss.hasPermi('civil:regularcheck:remove')")
    @Log(title = "土建结构定期检查", businessType = BusinessType.DELETE)
	@DeleteMapping("/{regularCheckIds}")
    public AjaxResult remove(@PathVariable Long[] regularCheckIds)
    {
        return toAjax(civilEnginneeringRegularCheckService.deleteCivilEnginneeringRegularCheckByRegularCheckIds(regularCheckIds));
    }

}
